package mya.mes.mdc.share.api;


import mya.mes.mdc.share.vo.MdcOptions;

import java.util.concurrent.atomic.AtomicBoolean;

public class MdcModule {

    private final static MdcModule _module = new MdcModule();
    /**
     * 确保只初始化一次
     */
    private static final AtomicBoolean initialized = new AtomicBoolean(false);

    private MdcServiceFactory _factory;


    private MdcInitService mdcInitService;

    private MdcOptions mdcOptions;

    private MdcModule() {
    }


    public void init(MdcOptions mdcOptions) throws Exception {

        synchronized (initialized) {
            if (initialized.compareAndSet(false, true)) {
//                mdcOptions.ALARM_SENT_TO_WEIXIN = false;
                setMdcOptions(mdcOptions);
                mdcInitService.init(mdcOptions);
            } else {
                throw new Exception("MDC 模块已经初始化，不能重复初始化!");
            }
        }

    }


    public MdcOptions getMdcOptions() {
        return mdcOptions;
    }

    public void setMdcOptions(MdcOptions mdcOptions) {
        this.mdcOptions = mdcOptions;
    }

    public static MdcModule getInstance() {
        return _module;
    }

    public static void initServiceFactory(MdcServiceFactory factory) {
        getInstance().setServiceFactory(factory);
    }


    private void setServiceFactory(MdcServiceFactory factory) {
        this._factory = factory;
    }

    /**
     * （模块内部使用）使用缺省值
     *
     * @return
     */
    public MdcInitService getMdcInitService() {
        return mdcInitService;
    }

    /**
     * （模块内部使用）使用缺省值
     *
     * @return
     */
    public void setMdcInitService(MdcInitService mdcInitService) {
        this.mdcInitService = mdcInitService;
    }

    public MdcService createMdcService() {
        return _factory.createMdcService();
    }
}